﻿using System.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Interchanger
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }


        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        private void button1_Click(object sender, EventArgs e)
        {
         
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           textBox2.Text= Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        }
        int length = 0;
        string Dir = "";
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Task test = new Task(() =>
            {

            
            if (Directory.Exists(textBox2.Text))
            {
                listBox1.Items.Clear();
                length = textBox2.Text.Length+1;
                Dir = textBox2.Text;
                pictureBox1.Enabled = false;
                double count = Directory.GetFiles(textBox2.Text).Length;
                double current = 0;
               
               panel1.Width = 0;


               // panel1.Left = ClientRectangle.Left - ClientRectangle.Width;

               // System.Threading.Thread.Sleep(2000);
                Task lol = new Task(() =>
                {

                
                foreach (string File in Directory.GetFiles(textBox2.Text).Select(x=>x))
                {
                    listBox1.Items.Add(File.Remove(0, length));
                    current +=1;
                   int percentage = Convert.ToInt16(current / count * 262);

                  
                     panel1.Width = percentage;
                      panel1.Height = 2;
                      panel1.Top = ClientRectangle.Top;
                      panel1.Left = ClientRectangle.Left;



                 ///  MessageBox.Show(percentage.ToString());
                  //  panel1.Left +=percentage;
                    
                }
                }); lol.Start(); lol.Wait();

                pictureBox1.Enabled = true;
            }
            }); test.Start();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Properties.Settings.Default.File = Dir+"\\"+listBox1.SelectedItem.ToString();
                this.Hide();
                Properties.Settings.Default.Action = true;

                Read LOL = new Read();
                LOL.Show();

                Task noWait = new Task(() =>
                {
                    WaitForAction();
                }); noWait.Start();


            }catch(Exception)
            {

            }
           
        }


        void WaitForAction()
        {
            Task Waiting = new Task(() =>
            {


                while (Properties.Settings.Default.Action == true)
                {
                    System.Threading.Thread.Sleep(870);
                   
                }
            }); Waiting.Start(); Waiting.Wait();
            this.Show();
        }

        private void panel2_MouseClick(object sender, MouseEventArgs e)
        {
            Environment.Exit(0);
        }

        private void panel3_MouseClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
